package br.usp.fsi.webservice;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//http://balusc.blogspot.com/2007/04/imageservlet.html

public class EstatisticasServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	// Constants ----------------------------------------------------------------------------------

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	        throws ServletException, IOException
	    {
	        // Get ID from request.
	        String imageId = request.getParameter("id");

	        // Check if ID is supplied to the request.
	        if (imageId == null) {
	            // if the ID is not supplied to the request.
	            // send 404.
	            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
	            return;
	        }

	        // Generate Image by ImageId.
	        Image image = new Image();

	        // Check if image is actually generated.
	        /*
	        if (image == null || image.getContent()==null) {
	        	// Do the thing if the image does not exist in database (default image).
	        	File imageNull = new File(getServletContext().getRealPath("/") + "estatisticas");
	        	String contentType = getServletContext().getMimeType(imageNull.getName());
	        	
	        	response.reset();
	            response.setBufferSize(DEFAULT_BUFFER_SIZE);
	            response.setContentType(contentType);
	            response.setHeader("Content-Length", String.valueOf(imageNull.length()));
	            response.setHeader("Content-Disposition", "inline; filename=\"" + imageNull.getName() + "\"");
	            
	            BufferedInputStream input = null;
	            BufferedOutputStream output = null;
	            
	            try {
	                input = new BufferedInputStream(new FileInputStream(imageNull), DEFAULT_BUFFER_SIZE);
	                output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

	                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
	                int length;
	                while ((length = input.read(buffer)) > 0) {
	                    output.write(buffer, 0, length);
	                }
	            } finally {
	                close(output);
	                close(input);
	            }
	            return;
	        }
	         */

	        // Init servlet response.
	        response.reset();
	        response.setBufferSize(DEFAULT_BUFFER_SIZE);
	        response.setContentType("image/png");

	        BufferedInputStream input = null;
	        BufferedOutputStream output = null;

	        try {
	            input = new BufferedInputStream(image.getContent(), DEFAULT_BUFFER_SIZE);
	            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

	            // Write file contents to response.
	            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
	            int length;
	            while ((length = input.read(buffer)) > 0) {
	                output.write(buffer, 0, length);
	            }
	        } finally {
	            close(output);
	            close(input);
	        }
	    }


	    private static void close(Closeable resource) {
	        if (resource != null) {
	            try {
	                resource.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
}
